/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MailViewPanel.java
 *
 * Created on 19-11-2011, 08:05:30 PM
 */
package mail.ui;

import java.awt.BorderLayout;
import mail.attribute.MailItem;
import mail.attribute.NullMailItem;
import mail.client.MailClient;
import mail.ui.event.MailItemEventListener;

/**
 *
 * @author ALEX
 */
public class MailViewPanel extends javax.swing.JPanel implements MailItemEventListener {

    private MailIterator mailIterator;//= new MailIteratorMock();
    private MailViewComponent mailView = new MailViewComponent();
    private MailClient mailClient;
    private MainScreen mainScreen;

    /** Creates new form MailViewPanel */
    public MailViewPanel(MailClient mailClient, MailIterator mailIterator, MainScreen mainScreen) {
        initComponents();
        this.mailIterator = mailIterator;
        this.mailClient = mailClient;
        this.mainScreen = mainScreen;
        this.setVisible(true);
        buttonPanel.setSize(550, 150);
        containerPanel.setSize(600, 200);
        containerPanel.add(mailView, BorderLayout.CENTER);
        mailIterator.addListener(this);
    }

    public void newEmail() {
        UINewMessage newMailView = new UINewMessage(null, true, mailClient, mainScreen);
        newMailView.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        containerPanel = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        btnDeleteAll = new javax.swing.JButton();
        btnNew = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        btnPrevious = new javax.swing.JButton();
        btnSwitch = new javax.swing.JButton();
        btnQuit = new javax.swing.JButton();
        btnForward = new javax.swing.JButton();
        btnReply = new javax.swing.JButton();

        containerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Read"));
        containerPanel.setLayout(new java.awt.BorderLayout());

        buttonPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnDeleteAll.setText("Delete all");
        btnDeleteAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteAllActionPerformed(evt);
            }
        });

        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        btnNext.setText(">");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnPrevious.setText("<");
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });

        btnSwitch.setText("Switch");
        btnSwitch.setEnabled(false);

        btnQuit.setText("Quit");
        btnQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitActionPerformed(evt);
            }
        });

        btnForward.setText("Reenviar");
        btnForward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnForwardActionPerformed(evt);
            }
        });

        btnReply.setText("Responder");
        btnReply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReplyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnDeleteAll, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNew)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnPrevious)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNext)
                .addGap(39, 39, 39)
                .addComponent(btnForward)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnReply)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSwitch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnQuit)
                .addContainerGap(263, Short.MAX_VALUE))
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDelete)
                    .addComponent(btnDeleteAll)
                    .addComponent(btnSwitch)
                    .addComponent(btnQuit)
                    .addComponent(btnForward)
                    .addComponent(btnReply)
                    .addComponent(btnNew)
                    .addComponent(btnPrevious)
                    .addComponent(btnNext))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(containerPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 919, Short.MAX_VALUE)
                    .addComponent(buttonPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(containerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        mailIterator.goNext();
        mailView.setMailItem(mailIterator.getCurrent());
        mainScreen.refresh();
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviousActionPerformed
        mailIterator.goPrevious();
        mailView.setMailItem(mailIterator.getCurrent());
        mainScreen.refresh();
    }//GEN-LAST:event_btnPreviousActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        if (isValidSelection()) {
            mailClient.deleteMailItem(mailIterator.getCurrent());
            mainScreen.refresh();
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
    newEmail();
}//GEN-LAST:event_btnNewActionPerformed

private void btnQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitActionPerformed
    System.exit(0);
}//GEN-LAST:event_btnQuitActionPerformed

private void btnDeleteAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteAllActionPerformed
    deleteAll();
}//GEN-LAST:event_btnDeleteAllActionPerformed

private void btnForwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnForwardActionPerformed
    forward();
}//GEN-LAST:event_btnForwardActionPerformed

private void btnReplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReplyActionPerformed
    reply();
}//GEN-LAST:event_btnReplyActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnDeleteAll;
    private javax.swing.JButton btnForward;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JButton btnQuit;
    private javax.swing.JButton btnReply;
    private javax.swing.JButton btnSwitch;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel containerPanel;
    // End of variables declaration//GEN-END:variables

    private void deleteAll() {
        ((MailListModel) mailIterator).deleteAllMails();
    }

    private void forward() {
        if (isValidSelection()) {
            MailItem current = mailIterator.getCurrent();
            UIForwardMessage dialog = new UIForwardMessage(mainScreen, current, mailClient, mainScreen);
            dialog.setVisible(true);
        }
    }

    private void reply() {
        if (isValidSelection()) {
            MailItem current = mailIterator.getCurrent();
            UIReplyMessage dialog = new UIReplyMessage(mainScreen, current, mailClient, mainScreen);
            dialog.setVisible(true);
        }
    }

    private boolean isValidSelection() {
        return !mailIterator.getCurrent().equals(NullMailItem.NULL_ITEM);
    }

    @Override
    public void itemChanged(MailItem item) {
        mailView.setMailItem(item);
    }
}
